<?php

namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class Admin extends Model
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = ['phone', 'email', 'avatar', 'status', 'username', 'role_id', 'login_time', 'wechat', 'admin_id'];
    public $timestamps = true;
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
        'publish_at' => 'datetime:Y-m-d H:i:s',
    ];
    protected $hidden = [
        'password',
    ];
    /**
     * Prepare a date for array / JSON serialization.
     *
     * @param  \DateTimeInterface  $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }


    /**
     * 获取用户的唯一标识符
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey(); // 假设主键是id字段
    }

    public function roles()
    {
        return $this->belongsTo(Role::class, 'role_id');
    }
    public function admins()
    {
        return $this->belongsTo(Admin::class, 'admin_id');
    }
    public function statuslabel()
    {
        return $this->belongsTo(Dicdata::class, 'status', 'value')
            ->where('typeCode', '=', 'user_status');
    }

    public function addresses()
    {
        return $this->hasMany(AdminAddress::class, 'admin_id');
    }
}
